package com.aliyun.timetask;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



import com.alibaba.fastjson.JSON;
import com.aliyun.bean.ResultAddress;
import com.aliyun.util.HttpClientUtil;
import com.aliyun.util.ReadConfig;

/**
 */
public class YuMingCheckWSTask {

	public void beginTask() {
		try {
			//改为从远程去获取数据
			ReadConfig re = new ReadConfig();
	        String url = re.getValue("wsurl");
	        String areacode = re.getValue("areacode");
	        url = url.replace("getYuMing", "getYuMingList");
	        HttpClientUtil hcUtil = HttpClientUtil.getInstance();
	        List<Map<String,Object>> list = hcUtil.getYuMingList(url+"?areacode="+areacode);
	        
	        if(list!=null && list.size()>0){
	        	for(Map<String,Object> map : list){
	        		ResultAddress ra = new ResultAddress();
	        		try{
	        			ra = hcUtil.checkInetAddressS(map);
	        		}catch(Exception e){
	        			System.out.println(map.get("name")+"---访问异常---");
	        			ra.setStatus("5");
	        		}
					String status = ra.getStatus();
					map.put("status", status);
					map.put("currentaddress", (ra.getLocation()==null||"".equals(ra.getLocation()))?map.get("name")+"":ra.getLocation());
					
					if(ra.getLocation()!=null&&!"".equals(ra.getLocation())&&(ra.getLocation().startsWith("http://"+map.get("name")+"")
							||ra.getLocation().startsWith("https://"+map.get("name")+"")
							||ra.getLocation().contains(map.get("name")+""))){
						map.put("status", "1");
						map.put("currentaddress", ra.getLocation().split("/robot")[0]);
						
					}
					if("5".equals(status)){
						map.put("status", "5");
						map.put("currentaddress", "");
						
					}
	        	}
	        }
	        //list转成json返回，更新数据
	        String returnStr=JSON.toJSONString(list);
	        Map<String,String> retrunMap = new HashMap<String,String>();
	        retrunMap.put("returnStr", returnStr);
	        url = url.replace("getYuMingList", "updateYuMingList");
	        hcUtil.updateYuMingList(url,retrunMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
